package com.javaee.response;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author fhzheng
 * <h1> 多参数处理  </h1>
 * <li> 单个参数，用字符串接收
 * <li> 多个参数，用字符串数组接收
 * <li> 单个参数，用字符串接收
 * 
 */
public class ParamsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ParamsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("<br>this is ParamsServlet...");
		// 设置输入的编码
		request.setCharacterEncoding("utf-8");
		// 单个参数
		String name = request.getParameter("username");
		String password = request.getParameter("password");
		// 设置输出类型和编码
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("用户名:" + name);
		out.println("<br />密码:" + password );
		
		// 获取参数名为“hobby”的值【里面可能会有多个值---构成数组结构】，输出到页面
		String[] hobbys = request.getParameterValues("hobby");
		out.print("<br />爱好:");
		if (hobbys == null) {
			out.println("没有爱好.");
		}else {
			for (int i = 0; i < hobbys.length; i++) {
				out.print(hobbys[i] + ", ");
			}
			// 最后怎么处理多的那个逗号？
		}
		out.println("<br />");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
